'use strict';
/**
 * @todo Cain���߰����Native����
 * @namespace Cain.bridge
 * @author haze.liu
 * @since 2016��7��15�� 12:34:22
 */
var bridge = {
	loginFlag: 'web',
	IOS: 'ios',
	ANDROID: 'android',
	WEB: 'web',
	/**
	* @private
	* @function
	* @todo ˽�г�ʼ��
	* @memberof Cain.bridge
	*
	*/
	_init: function () {
		// =====以下是iOS必须的特殊处理========

		// =====以上是iOS必须的特殊处理========
		//		this.loginFlag = Cain.getParam('loginFlag') || 'web';
		var WVJBIframe = document.createElement('iframe');
		WVJBIframe.style.display = 'none';
		WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
		document.documentElement.appendChild(WVJBIframe);

		localStorage.setItem("loginFlag", this.loginFlag);
	},
	/**
	* @public
	* @function
	* @todo ����ǰ��
	* @memberof Cain.bridge
	*
	*/
	start: function (callback) {
		if (window.WebViewJavascriptBridge) {
			callback(WebViewJavascriptBridge)
		} else {
			document.addEventListener('WebViewJavascriptBridgeReady', function () {
				callback(WebViewJavascriptBridge)
			}, false)
		}
		if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
		window.WVJBCallbacks = [callback];
		var WVJBIframe = document.createElement('iframe');
		WVJBIframe.style.display = 'none';
		WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
		document.documentElement.appendChild(WVJBIframe);
		setTimeout(function () { document.documentElement.removeChild(WVJBIframe) }, 0);
	},
	/**
	* @public
	* @function
	* @todo ��ʼ��
	* @memberof Cain.bridge
	* @param [JSON] param - ����
	* 				{
	* 				appCallBack(flag) ->APP����  flag ->1 ios ,2 android
	* 			    webCallBack ->Web����
	* 				commonCallBack ->ͨ��
	* }
	*/
	init: function (param) {
		var p = {
			//			iosCallback:function(){},
			//			androidCallback:function(){},
			appCallBack: function () { },
			webCallBack: function () { },
			commonCallBack: function () { }
		};
		$.extend(p, param);


		switch (this.loginFlag) {
			case this.IOS: //ios
			case this.ANDROID: //android
				p.appCallBack(this.loginFlag);
				break;
			case this.WEB:  //web
				p.webCallBack();
				break;
		}
		p.commonCallBack();

	},
	/**
	* @public
	* @function
	* @todo ���øı�Title
	* @memberof Cain.bridge
	*
	*/
	callSetTitle: function (title) {
		// console.log(title);
		bridge.start(function (bridge) {
			bridge.callHandler('_app_setTitle', title);
		});
	},
	callChooseImage: function (data) {
		// console.log(title);
		bridge.start(function (bridge) {
			bridge.callHandler('Uama_chooseImage', {type:0,maxCount:9});
		});
	}
}

$(document).ready(function () {
	bridge._init();
	// bridge.callSetTitle("幸福生活秀");
});
export { bridge };


